package model.inventory;

import java.text.MessageFormat;
import java.util.Arrays;

import org.json.simple.JSONObject;

public class InventoryTableDefinition {
	private static final int INVALID_CONTAINER_ID = -1;
	private int numberOfTableRows;
	private int tableRowHeight;
	private String[] tableFields;
	private int tableContainerId;
	private FieldFormatMapper tableFieldFormatMapper;

	public InventoryTableDefinition() {
		this.resetTableDefinition();
	}

	public void resetTableDefinition() {
		numberOfTableRows = 5;
		tableRowHeight = 50;
		tableFields = new String[] {};
		tableContainerId = INVALID_CONTAINER_ID;
		tableFieldFormatMapper = new FieldFormatMapper();
	}

	public int getNumberOfTableRows() {
		return numberOfTableRows;
	}

	public void setNumberOfTableRows(int numberOfTableRows) {
		this.numberOfTableRows = numberOfTableRows;
	}

	public int getTableRowHeight() {
		return tableRowHeight;
	}

	public void setTableRowHeight(int tableRowHeight) {
		this.tableRowHeight = tableRowHeight;
	}

	public String[] getTableFields() {
		return tableFields;
	}

	public void setTableFields(String[] tableFields) {
		this.tableFields = tableFields;
	}

	public int getTableContainerId() {
		return tableContainerId;
	}

	public void setTableContainerId(int tableContainerId) {
		this.tableContainerId = tableContainerId;
	}

	public void addTableFieldFormat(String fieldName, JSONObject format) {
		this.tableFieldFormatMapper.put(fieldName, format);
	}
	
	public void removeTableFieldFormat(String fieldName) {
		this.tableFieldFormatMapper.removeMapping(fieldName);
	}

	public boolean isValidTableDefinition() {
		return numberOfTableRows>0 && tableFields.length>0 && tableContainerId!=INVALID_CONTAINER_ID && tableFieldFormatMapper.size()>0;
	}
	
	public JSONObject getFormat(String fieldName) {
		return this.tableFieldFormatMapper.get(fieldName);
	}
	
	public String toString() {
		Object[] values = {this.numberOfTableRows, tableRowHeight, tableContainerId, Arrays.toString(tableFields)};
		String pattern = "Inventory Table({2}): RowHeight({1}), NumberOfRows({0}), fields: {3}";
		return MessageFormat.format(pattern, values);
	}
}
